//
//  DateDetailVC.m
//  MovieDate
//
//  Created by yyta on 16/3/27.
//  Copyright © 2016年 MovieDate. All rights reserved.
//

#import "DateDetailVC.h"
#import "TDJScrollAdView.h"
#import "WarringVC.h"

@interface DateDetailVC () <TDJScrollViewDataSource, TDJScrollViewDelegate> {
    TDJScrollAdView *srcView;
    
    NSMutableArray *pArrayUserPic;
}

@property (weak, nonatomic) IBOutlet UILabel *pLblName;
@property (weak, nonatomic) IBOutlet UIImageView *pImgSex;
@property (weak, nonatomic) IBOutlet UILabel *pLblAge;
@property (weak, nonatomic) IBOutlet UILabel *pLblTime;
@property (weak, nonatomic) IBOutlet UILabel *pLblAddress;
@property (weak, nonatomic) IBOutlet UILabel *pLblDescription;

@property (weak, nonatomic) IBOutlet UIView *pViewHeader;
@property (weak, nonatomic) IBOutlet UIButton *pBtnJoin;

@end

@implementation DateDetailVC

-(void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden = YES;
}

- (void)initInfo
{
    [super initInfo];
    NSString *uid = [NSString stringWithFormat:@"%@",[_pDict valueForKey:@"uid"]];
    User *user = [User defaultUser];
    NSString *useruid = [NSString stringWithFormat:@"%@",user.uid];
    if ([[self.pDict objectForKey:@"btnHidden"] isEqualToString:@"2"]) {
        [self.pBtnJoin setHidden:YES];
    } else {
        [self.pBtnJoin setHidden:NO];
    }
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    [_pBtnJoin.layer setCornerRadius:5.0f];
    [_pBtnJoin.layer setMasksToBounds:YES];
    if ([uid isEqualToString:useruid]) {
        _pBtnJoin.backgroundColor = [UIColor grayColor];
        _pBtnJoin.userInteractionEnabled = NO;
    }
    [_pLblName setText:[_pDict objectForKey:@"nickname"]];
    [_pImgSex setImage:[UIImage imageNamed:[[_pDict objectForKey:@"userSex"] isEqualToString:@"1"]?@"sex_boy":@"sex_girl"]];
    [_pLblAge setText:[_pDict objectForKey:@"userAge"]];
    if ([[_pDict objectForKey:@"userSex"] isEqualToString:@"1"]) {
        [_pLblAge setTextColor:[UIColor colorWithRed:108/255.0f green:181/255.0f blue:255/255.0f alpha:1]];
    } else {
        [_pLblAge setTextColor:[UIColor colorWithRed:252/255.0f green:110/255.0f blue:108/255.0f alpha:1]];
    }
    
    [_pLblTime setText:[_pDict objectForKey:@"releaseTime"]];
    
    NSString *strAddress = [_pDict valueForKey:@"releaseAddress"];
    @try {
        if (!strAddress || [strAddress rangeOfString:@"null"].length > 0) {
            strAddress = [User defaultUser].pStrLocationAddress;
        }
        [_pLblAddress setText:strAddress];
    } @catch (NSException *exception) {
    } @finally {
    }

    [_pLblDescription setText:[NSString stringWithFormat:@"付款方式:%@", [[_pDict objectForKey:@"payType"] intValue]==1?@"我付":([[_pDict objectForKey:@"payType"] intValue]==1?@"你付":@"AA制")]];
    
    if (!pArrayUserPic) {
        pArrayUserPic = [NSMutableArray array];
    }
    [pArrayUserPic addObject:[NSString stringWithFormat:@"%@%@", IP, [_pDict objectForKey:@"headImage"]]];
    
    [self.pViewHeader setFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.width)];
    self.pTableView.tableHeaderView = self.pViewHeader;
    srcView = [[TDJScrollAdView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.width)];
    [srcView setDataSource:self];
    [srcView setDelegate:self];
    [srcView.timerScroll invalidate];
    [self.pViewHeader addSubview:srcView];
    [srcView reloadData];
}

- (IBAction)btnPopAction:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}

- (IBAction)btnWarringAction:(UIButton *)sender {
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Warring" bundle:nil];
    WarringVC *warringVC = [storyboard instantiateViewControllerWithIdentifier:@"WarringVC"];
    warringVC.pStrID = [_pDict valueForKey:@"uid"];
    [self.navigationController pushViewController:warringVC animated:YES];
}


#pragma mark - TDJScrollViewDataSource, TDJScrollViewDelegate

- (NSInteger)numberOfPages {
    return pArrayUserPic.count;
}

- (UIView *)pageAtIndex:(NSInteger)index
{
    UIImageView *imgPic = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, srcView.frame.size.height)];
    [imgPic sd_setImageWithURL:[NSURL URLWithString:[pArrayUserPic objectAtIndex:index]]];
    
    return imgPic;
}

- (void)didClickPage:(TDJScrollAdView *)view atIndex:(NSInteger)index {
}


- (IBAction)btnAcceptAction:(UIButton *)sender
{
    if ([[_pDict objectForKey:@"appId"] intValue] == [[User defaultUser].uid intValue]) {
        [SVProgressHUD showErrorWithStatus:@"不能与应约自己的影约"];
        return;
    }
    
    NSDate *date = [NSDate date];
    NSTimeZone *zone = [NSTimeZone systemTimeZone];
    NSInteger interval = [zone secondsFromGMTForDate:date];
    NSDate *localeDate = [date  dateByAddingTimeInterval:interval];
    NSString *strDate = [NSString stringWithFormat:@"%@", localeDate];
    [SVProgressHUD show];
    [TDNetwork requestAccept:MY_Accept acceptTime:[strDate substringToIndex:10] acceptAddress:[User defaultUser].pStrLocationAddress appId:[_pDict objectForKey:@"appId"] releaseTime:[_pDict objectForKey:@"releaseTime"] complete:^(BOOL succeed, NSString *ret, NSDictionary *dictionary) {
        if (succeed) {
            [SVProgressHUD showSuccessWithStatus:@"提交成功"];
            [self btnPopAction:nil];
        }
    }];
    
}
////关注接口

-(void)attentionAction:(NSString *)uid{
    [SVProgressHUD show];
[TDNetwork  requestCollection:MY_COLLECTION_List toUid: [[_pDict valueForKey:@"toUid"] integerValue]complete:^(BOOL succeed, NSString *ret, NSDictionary *dictionary) {
    if (succeed) {
        if ([ret isEqualToString:@"1"]) {
            
            [SVProgressHUD showSuccessWithStatus:@"关注成功"];
        }else{
            
            [SVProgressHUD showSuccessWithStatus:@"未知错误"];
        }
    }else{
    
        [SVProgressHUD showErrorWithStatus:@"网络请求失败"];
    }
}];

}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
